/*
 * Copyright (C) 2000-2008 Tan Menglong <TanMenglong@gmail.com>
 * 
 * This code is distributed under GNU Public License 2.0. Follow this URL for details.
 * http://www.opensource.org/licenses/gpl-license.phpl
 * 
 */

package net.ziqiang.sdk.ibatishelper.dao;

/*
 * Implemention of generic iBatis methods interface
 * Author: Tan Menglong<TanMenglong@gmail.com>
 * Version: 0.04
 * Last Modified: 26-Jul-08
 * Description: Implemention of generic iBatis methods interface. All database common methods should implement this interface.
 */
import java.io.Reader;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

/**
 * <p>
 * Generic iBatis DAO methods.
 * </p>
 * 
 * @author TanMenglong <a href="mailto:TanMenglong@gmail.com">TanMenglong@gmail.com</a>
 * @version 0.004
 */
public class CommonDaoImpl implements CommonDao{
	private SqlMapClient sqlMapClient=null;
	
	public CommonDaoImpl(){}

	/**
	 * Constructor for CommonDao
	 * @param resource configure xml file in build path
	 */
	public CommonDaoImpl(String resource){
		try{
			Reader reader = Resources.getResourceAsReader(resource); 
			this.sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		}catch(Exception e){
			e.printStackTrace(); 
			throw new RuntimeException ("Error initializing SqlMapClient . Cause: "+e); 
		}
	}
	
	/**
	 * Set resource,especially useful in spring.
	 * @param resource configure xml file in build path
	 */
	public void setResource(String resource){
		try{
			Reader reader = Resources.getResourceAsReader(resource); 
			this.sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		}catch(Exception e){
			e.printStackTrace(); 
			throw new RuntimeException ("Error initializing SqlMapClient . Cause: "+e); 
		}
	}
	
	/**
	 * Return an Object
	 * @param sqlType SQL id
	 */
	@SuppressWarnings("unchecked")
	public <R> R get(String sqlType) throws RuntimeException {
		try{
			return (R)sqlMapClient.queryForObject(sqlType);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * Return an Object with specific parameter
	 * @param sqlType SQL id
	 */
	@SuppressWarnings("unchecked")
	public <R, P> R getByParameter(P parameter, String sqlType)
			throws RuntimeException {
		try{
			return (R)sqlMapClient.queryForObject(sqlType,parameter);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * Return a List
	 * @param sqlType SQL id
	 */
	@SuppressWarnings("unchecked")
	public <R> List<R> getList(String sqlType) throws RuntimeException {
		List<R> alist=null;
		try{
			alist=sqlMapClient.queryForList(sqlType);
			return alist;
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * Return a list with specific parameter
	 * @param sqlType SQL id
	 */
	@SuppressWarnings("unchecked")
	public <R, P> List<R> getListByParameter(P parameter, String sqlType)
			throws RuntimeException {
		List<R> alist=null;
		try{
			alist=sqlMapClient.queryForList(sqlType,parameter);
			return alist;
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	
	/**
	 * Execute a delete
	 * @param sqlType SQL id
	 */
	public boolean delete(String sqlType) {
		try{
			sqlMapClient.delete(sqlType);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * Execute a delete with specific parameter
	 * @param sqlType SQL id
	 */
	public <P> boolean deleteByParameter(P parameter, String sqlType) {
		try{
			sqlMapClient.delete(sqlType,parameter);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * Execute a insert
	 * @param sqlType SQL id
	 */
	public boolean insert(String sqlType) {
		try{
			sqlMapClient.insert(sqlType);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return true;
	}

	/**
	 * Execute a insert with specific parameter
	 * @param sqlType SQL id
	 */
	public <P> boolean insertByParameter(P parameter, String sqlType) {
		try{
			sqlMapClient.insert(sqlType,parameter);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return true;
	}

	/**
	 * Execute a insert with a return value
	 * Especially used in returning primary key value
	 * @param sqlType SQL id
	 */
	@SuppressWarnings("unchecked")
	public <R> R insert2(String sqlType) {
		try{
			return (R)sqlMapClient.insert(sqlType);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * Execute a insert with specific parameter and return value
	 * Especially used in returning primary key value
	 * @param sqlType SQL id
	 */
	@SuppressWarnings("unchecked")
	public <R, P> R insertByParameter2(P parameter, String sqlType) {
		try{
			return (R)sqlMapClient.insert(sqlType,parameter);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
	}
	
	/**
	 * Execute a update
	 * @param sqlType SQL id
	 */
	public boolean update(String sqlType) {
		try{
			sqlMapClient.update(sqlType);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return true;
	}

	/**
	 * Execute a update with specific parameter
	 * @param sqlType SQL id
	 */
	public <P> boolean updateByParameter(P parameter, String sqlType) {
		try{
			sqlMapClient.update(sqlType,parameter);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		return true;
	}


}
